﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Reflection;

namespace UserLimitedGroupSelection.Code
{
    public class UserLimitedGroupSelection : SPFieldUser
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UserLimitedGroupSelection" /> class.
        /// </summary>
        /// <param name="fields">The fields.</param>
        /// <param name="fieldName">Name of the field.</param>
        public UserLimitedGroupSelection(SPFieldCollection fields, string fieldName)
            : base(fields, fieldName)
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UserLimitedGroupSelection" /> class.
        /// </summary>
        /// <param name="fields">The fields.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="displayName">The display name.</param>
        public UserLimitedGroupSelection(SPFieldCollection fields, string fieldName, string displayName)
            : base(fields, fieldName, displayName)
        {

        }

        /// <summary>
        /// Gets the control that is used to render the field.
        /// </summary>
        /// <returns>An <see cref="T:Microsoft.SharePoint.WebControls.BaseFieldControl" /> object that represents the rendering control.</returns>
        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                return new UserLimitedGroupSelectionControl
                {
                    FieldName = base.InternalName,
                };
            }
        }

        /// <summary>
        /// Gets the name of the group.
        /// </summary>
        /// <value>
        /// The name of the group.
        /// </value>
        public string GroupName
        {
            get { return (string)this.GetCustomProperty("GroupName"); }
        }

        /// <summary>
        /// Gets a value indicating whether [show users].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [show users]; otherwise, <c>false</c>.
        /// </value>
        public bool ShowUsers
        {
            get { return (bool)GetCustomProperty("ShowUsers"); }
        }

        /// <summary>
        /// Gets a value indicating whether [show groups].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [show groups]; otherwise, <c>false</c>.
        /// </value>
        public bool ShowGroups
        {
            get { return (bool)GetCustomProperty("ShowGroups"); }
        }

        /// <summary>
        /// Updates the database with changes that are made to the field.
        /// </summary>
        public override void Update()
        {
            //Update properties and set internal variabeles
            AllowMultipleValues = (bool)GetCustomProperty("AllowMultipleValues");
            SelectionMode = SPFieldUserSelectionMode.PeopleAndGroups;

            //base.SetFieldAttributeValue("Type", this.InternalTypeForMultiValues);
            typeof(SPField).GetMethod("SetFieldAttributeValue", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod).Invoke(this, new object[] { "Type", "UserLimitedGroupSelection" });

            //base.SetFieldAttributeValue("List", "UserInfo");
            typeof(SPField).GetMethod("SetFieldAttributeValue", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod).Invoke(this, new object[] { "List", "UserInfo" });

            base.Update();
        }
    }
}